<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sign extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->model("sign_model");
	}
	public function index()
	{
		$data['success'] = $this->session->flashdata('success');
		$data['error'] = '';
		$data['in_error'] = '';
		$this->load->view('sign.tpl',$data);
	}

	public function up(){
		$data['success'] = '';
		$data['error'] = '';
		$data['in_error'] = '';
		$this->form_validation->set_rules('email2', '邮箱', 'trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('password2', '密码', 'trim|required|xss_clean|min_length[6]|max_length[20]|alpha_dash');
		$this->form_validation->set_rules('name', '昵称', 'trim|required|xss_clean');
		$this->form_validation->set_rules('repw', '确认密码', 'trim|required|xss_clean|min_length[6]|max_length[20]|alpha_dash|matches[password]');
		if ($this->form_validation->run()) {								// validation ok
			$register = $this->sign_model->signup($this->form_validation->set_value('email2'),
						$this->form_validation->set_value('password2'),
						$this->form_validation->set_value('name')
						);
			if($register['status']){
				$this->session->set_flashdata('success', '注册成功，请登录！');
				redirect('/sign');
			}else{
				$data['error'] = $register['msg'];
			}
		}
		$this->load->view('sign.tpl',$data);
	}

	public function in(){
		$data['success'] = '';
		$data['error'] = '';
		$data['in_error'] = '';
		$data['url'] = '';
		$this->form_validation->set_rules('email', '邮箱', 'trim|required|xss_clean|valid_email');
		$this->form_validation->set_rules('password', '密码', 'trim|required|xss_clean|min_length[6]|max_length[20]|alpha_dash');
		if(isset($_GET['url'])){
			$data['url'] = $_GET['url'];
		}
		if ($this->form_validation->run()) {								// validation ok
			$login = $this->sign_model->signin($this->form_validation->set_value('email'),
						$this->form_validation->set_value('password')
						);
			if($login['status']){
				$redirect = $this->session->userdata('redirect');
				$redirect = $redirect ? $redirect : '/';
				if(isset($_GET['url'])){
					$redirect= $_GET['url'];
				}
				redirect($redirect);
			}else{
				$data['in_error'] = $login['msg'];
			}
		}
		$this->load->view('sign.tpl',$data);
	}


	public function out(){
		$this->session->sess_destroy();
		redirect('/');
	}
}

/* End of file Sign.php */
/* Location: ./app/controllers/Sign.php */